package com.ruoyi.web.controller.sxt;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.core.domain.entity.SxtCaidan;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.sxt.service.ISxtCaidanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜单权限Controller
 * 
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/sxt/caidan")
public class SxtCaidanController extends BaseController
{
    @Autowired
    private ISxtCaidanService sxtCaidanService;

    /**
     * 查询菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:list')")
    @GetMapping("/list")
    public TableDataInfo list(SxtCaidan sxtCaidan)
    {
        //startPage();
        List<SxtCaidan> list = sxtCaidanService.selectSxtCaidanList(sxtCaidan);
        return getDataTable(list);
    }

    /**
     * 查询菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:list')")
    @GetMapping("/menu_list")
    public TableDataInfo menuList(SxtCaidan sxtCaidan)
    {
        List<SxtCaidan> list = sxtCaidanService.selectSxtCaidanList(sxtCaidan);
        return getDataTable(sxtCaidanService.buildCaidanTreeSelect(list));
    }

    /**
     * 查询选中的角色菜单列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:list')")
    @GetMapping("/role_caidan")
    public AjaxResult jueseCaidanList(Long roleId)
    {
        SxtCaidan sxtCaidan = new SxtCaidan();
        List<SxtCaidan> caidanList = sxtCaidanService.selectSxtCaidanList(sxtCaidan);
        AjaxResult ajax = AjaxResult.success();
        List<Long> jueseCaidanIds = sxtCaidanService.selectJueseCaidanListById(roleId);
        ajax.put("checkedKeys", jueseCaidanIds);
        ajax.put("menus", sxtCaidanService.buildCaidanTreeSelect(caidanList));
        return ajax;
    }


    /**
     * 导出菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:export')")
    @Log(title = "菜单权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SxtCaidan sxtCaidan)
    {
        List<SxtCaidan> list = sxtCaidanService.selectSxtCaidanList(sxtCaidan);
        ExcelUtil<SxtCaidan> util = new ExcelUtil<SxtCaidan>(SxtCaidan.class);
        util.exportExcel(response, list, "菜单权限数据");
    }

    /**
     * 获取菜单权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:query')")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable("menuId") Long menuId)
    {
        return success(sxtCaidanService.selectSxtCaidanByMenuId(menuId));
    }

    /**
     * 新增菜单权限
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:add')")
    @Log(title = "菜单权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SxtCaidan sxtCaidan)
    {
        return toAjax(sxtCaidanService.insertSxtCaidan(sxtCaidan));
    }

    /**
     * 修改菜单权限
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:edit')")
    @Log(title = "菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SxtCaidan sxtCaidan)
    {
        return toAjax(sxtCaidanService.updateSxtCaidan(sxtCaidan));
    }

    /**
     * 删除菜单权限
     */
    @PreAuthorize("@ss.hasPermi('sxt:caidan:remove')")
    @Log(title = "菜单权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{menuIds}")
    public AjaxResult remove(@PathVariable Long[] menuIds)
    {
        return toAjax(sxtCaidanService.deleteSxtCaidanByMenuIds(menuIds));
    }
}
